from flask import Flask, Blueprint
from flask_cors import CORS

from myapp.api.v1 import GPTAPI, UserAPI
from myapp.config import DBconfig
from myapp.exts import db


def create_app():
    app = Flask(__name__, static_folder='../static')
    CORS(app)
    register_blueprint(app)
    # 导入数据库配置
    app.config.from_object(DBconfig)
    # 创建数据库 sqlalchemy 工具对象
    db.init_app(app)
    return app


def create_blueprint():
    v1 = Blueprint("v1", __name__)
    GPTAPI.api.register(v1)
    UserAPI.api.register(v1)
    return v1


def register_blueprint(app):
    bp_v1 = create_blueprint()
    app.register_blueprint(bp_v1, url_prefix="/v1")  # url_prefix是给注册的蓝图添加路由前缀。
